package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.MildCaseService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.MildCaseDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing MildCase.
 */
@RestController
@RequestMapping("/api")
public class MildCaseResource {

    private final Logger log = LoggerFactory.getLogger(MildCaseResource.class);

    private static final String ENTITY_NAME = "mildCase";

    private final MildCaseService mildCaseService;

    public MildCaseResource(MildCaseService mildCaseService) {
        this.mildCaseService = mildCaseService;
    }

    /**
     * POST  /mild-cases : Create a new mildCase.
     *
     * @param mildCaseDTO the mildCaseDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new mildCaseDTO, or with status 400 (Bad Request) if the mildCase has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/mild-cases")
    @Timed
    public ResponseEntity<MildCaseDTO> createMildCase(@RequestBody MildCaseDTO mildCaseDTO) throws URISyntaxException {
        log.debug("REST request to save MildCase : {}", mildCaseDTO);
        if (mildCaseDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new mildCase cannot already have an ID")).body(null);
        }
        MildCaseDTO result = mildCaseService.save(mildCaseDTO);
        return ResponseEntity.created(new URI("/api/mild-cases/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /mild-cases : Updates an existing mildCase.
     *
     * @param mildCaseDTO the mildCaseDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated mildCaseDTO,
     * or with status 400 (Bad Request) if the mildCaseDTO is not valid,
     * or with status 500 (Internal Server Error) if the mildCaseDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/mild-cases")
    @Timed
    public ResponseEntity<MildCaseDTO> updateMildCase(@RequestBody MildCaseDTO mildCaseDTO) throws URISyntaxException {
        log.debug("REST request to update MildCase : {}", mildCaseDTO);
        if (mildCaseDTO.getId() == null) {
            return createMildCase(mildCaseDTO);
        }
        MildCaseDTO result = mildCaseService.save(mildCaseDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, mildCaseDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /mild-cases : get all the mildCases.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of mildCases in body
     */
    @GetMapping("/mild-cases")
    @Timed
    public ResponseEntity<List<MildCaseDTO>> getAllMildCases(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of MildCases");
        Page<MildCaseDTO> page = mildCaseService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/mild-cases");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /mild-cases/:id : get the "id" mildCase.
     *
     * @param id the id of the mildCaseDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the mildCaseDTO, or with status 404 (Not Found)
     */
    @GetMapping("/mild-cases/{id}")
    @Timed
    public ResponseEntity<MildCaseDTO> getMildCase(@PathVariable Long id) {
        log.debug("REST request to get MildCase : {}", id);
        MildCaseDTO mildCaseDTO = mildCaseService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(mildCaseDTO));
    }

    /**
     * DELETE  /mild-cases/:id : delete the "id" mildCase.
     *
     * @param id the id of the mildCaseDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/mild-cases/{id}")
    @Timed
    public ResponseEntity<Void> deleteMildCase(@PathVariable Long id) {
        log.debug("REST request to delete MildCase : {}", id);
        mildCaseService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/mild-cases?query=:query : search for the mildCase corresponding
     * to the query.
     *
     * @param query the query of the mildCase search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/mild-cases")
    @Timed
    public ResponseEntity<List<MildCaseDTO>> searchMildCases(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of MildCases for query {}", query);
        Page<MildCaseDTO> page = mildCaseService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/mild-cases");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
